	package com.dx.mes.service.plan.service;


import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_INQUIRE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.dx.mes.common.bean.def.GenericDef.E_FDPAPLYSO_ALREADY_EXIST;
import static com.dx.mes.common.bean.def.GenericDef.E_FDPAPLYSO_BIND_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.E_FDPAPLYSO_DELETE_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.E_FDPAPLYSO_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.E_FDPAPLYSO_OTHER_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.E_FDPAPLYSO_STAT_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FDPAPLYSO;
import static com.dx.mes.common.bean.def.GenericDef.WO_STATUS_WAIT;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_mdl_def;
import com.dx.common.table.ppt.Ret_wo;
import com.dx.mes.common.dao.IBisMdlDefDao;
import com.dx.mes.common.dao.IRetWODao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.plan.service.IFdpaplysoService;
import com.dx.mes.facade.plan.tx.Fdpaplyso.FdpaplysoI;
import com.dx.mes.facade.plan.tx.Fdpaplyso.FdpaplysoIA;
import com.dx.mes.facade.plan.tx.Fdpaplyso.FdpaplysoO;
import com.dx.mes.facade.plan.tx.Fdpaplyso.FdpaplysoOA;


@Service("fdpaplysoService")
@SuppressWarnings({"unchecked"})
public class Fdpaplysoservice implements IFdpaplysoService{
	private static final String TX_NAME = T_FDPAPLYSO;
	private LogUtils logUtils;
	
	@Autowired
	private IRetWODao retWODao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	
	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(Fdpaplysoservice.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(Fdpaplysoservice.class);
		logUtils.info("[InTrx:" + strInTrx + "]");
		String strOutTrx="";
		FdpaplysoI inTrx = new FdpaplysoI();
		FdpaplysoO outTrx = new FdpaplysoO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);	
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		
		try {
			inTrx = JacksonUtil.fromJson(strInTrx, FdpaplysoI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByConditionFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_ADD:
				addFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_UPDATE:
				updateFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_DELETE:
				deleteFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_INQUIRE:
				queryByMtrl(inTrx,outTrx);
				break;
			case 'B' :
				bindSoId(inTrx,outTrx);
				break;
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retWODao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}
	
	private void queryByMtrl(FdpaplysoI inTrx, FdpaplysoO outTrx) {
		List<FdpaplysoIA> iary=inTrx.getIary();
		String mtrl_prod_id_fk = iary.get(0).getMtrl_prod_id_fk();
		String cus_id_fk = iary.get(0).getCus_id_fk();
		String from_thickness = iary.get(0).getFrom_thickness();
		String wo_cate = iary.get(0).getWo_cate();
		List<FdpaplysoOA> oaryList = new ArrayList<FdpaplysoOA>();
		String hql = "FROM Ret_wo where 1=1 and mtrl_prod_id_fk = '"+mtrl_prod_id_fk+
				"' and cus_id_fk ='"+cus_id_fk+"' and from_thickness='"+from_thickness+
				"' and wo_cate='"+wo_cate+"' order by wo_prty desc";
		List<Ret_wo> ret_wos=retWODao.find(hql);
		if(null != ret_wos && ret_wos.size()>0){
			FdpaplysoOA fdpaplysoOA = new FdpaplysoOA();
			fdpaplysoOA.setWo_id(ret_wos.get(0).getWo_id());
			fdpaplysoOA.setMdl_id_fk(ret_wos.get(0).getMdl_id_fk());
			fdpaplysoOA.setPln_prd_qty(ret_wos.get(0).getPln_prd_qty());
			oaryList.add(fdpaplysoOA);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
	}

	public void queryByConditionFnc(FdpaplysoI inTrx,FdpaplysoO outTrx) {
		List<FdpaplysoIA> iary=inTrx.getIary();
		String cusIdFk=iary.get(0).getCus_id_fk();
		String soId=iary.get(0).getSo_id();
		String woId = iary.get(0).getWo_id();
		String woCate=iary.get(0).getWo_cate();
		String mtrlProdIdFk=iary.get(0).getMtrl_prod_id_fk();
		String mdlIdFk=iary.get(0).getMdl_id_fk();
		String mtrlLotIdFk=iary.get(0).getMtrl_lot_id_fk();
		String woStat = iary.get(0).getWo_stat();
		String woTyp = "S";
		List<FdpaplysoOA> oaryList = new ArrayList<FdpaplysoOA>();
		
		String hql = "FROM Ret_wo where 1=1 ";
		if (cusIdFk != null && cusIdFk.length() != 0) {
				hql = hql + "and cus_id_fk='" + cusIdFk + "'";
		}
		if (woId != null  && woId.length() != 0) {
			hql = hql + "and wo_id='" + woId + "'";
		}
		if (woCate != null && woCate.length() != 0) {
			hql = hql + "and wo_cate='" + woCate + "'";
		}
		if (mtrlProdIdFk != null && mtrlProdIdFk.length() != 0) {
			hql = hql + "and mtrl_prod_id_fk='" + mtrlProdIdFk + "'";
		}
		if (mdlIdFk != null && mdlIdFk.length() != 0) {
			hql = hql + "and mdl_id_fk='" + mdlIdFk + "'";
		}
		if (mtrlLotIdFk != null && mtrlLotIdFk.length() != 0) {
			hql = hql + "and mtrl_lot_id_fk='" + mtrlLotIdFk + "'";
		}
		if (woStat != null && woStat.length() != 0) {
			hql = hql + "and wo_stat='" + woStat + "'";
		}
		if(soId != null && soId.length() != 0){
			hql = hql + "and so_id='" + soId + "'";
		}
		hql = hql + " and wo_typ ='" + woTyp+"'";
		
		List<Ret_wo> ret_wos=retWODao.find(hql);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示毫秒
		for (Ret_wo ret_wo : ret_wos) {
			FdpaplysoOA fdpaplysoOA = new FdpaplysoOA();
			if(ret_wo.getCrt_timestamp()!=null){
				fdpaplysoOA.setCrt_timestamp((df.format(ret_wo.getCrt_timestamp()).toString()));
			}
			fdpaplysoOA.setWo_stat(ret_wo.getWo_stat());
			fdpaplysoOA.setCus_id_fk(ret_wo.getCus_id_fk());
			fdpaplysoOA.setWo_id(ret_wo.getWo_id());
			fdpaplysoOA.setSo_id(ret_wo.getSo_id());
			fdpaplysoOA.setMtrl_prod_id_fk(ret_wo.getMtrl_prod_id_fk());
			if(ret_wo.getFrom_thickness()!=null){
				fdpaplysoOA.setFrom_thickness(ret_wo.getFrom_thickness().toString());
			}
			fdpaplysoOA.setMdl_id_fk(ret_wo.getMdl_id_fk());
			fdpaplysoOA.setPln_prd_qty(ret_wo.getPln_prd_qty());//计划数量
			fdpaplysoOA.setRl_prd_qty(ret_wo.getRl_prd_qty());//下线数量
			fdpaplysoOA.setComp_prd_qty(ret_wo.getComp_prd_qty());//完成数量
			fdpaplysoOA.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty());//入库数量
			fdpaplysoOA.setWh_out_prd_qty(ret_wo.getWh_out_prd_qty());//出库数量
			fdpaplysoOA.setScrp_prd_qty(ret_wo.getScrp_prd_qty());//报废数量
			if(ret_wo.getPln_cmp_timestamp()!=null){
				fdpaplysoOA.setPln_cmp_timestamp(df.format(ret_wo.getPln_cmp_timestamp()));
			}
			if(ret_wo.getPln_stb_timestamp()!=null){
				fdpaplysoOA.setPln_stb_timestamp(df.format(ret_wo.getPln_stb_timestamp()));
			}
			fdpaplysoOA.setWo_cate(ret_wo.getWo_cate());
			fdpaplysoOA.setMtrl_lot_id_fk(ret_wo.getMtrl_lot_id_fk());
			fdpaplysoOA.setWo_note(ret_wo.getWo_note());
			fdpaplysoOA.setWo_dsc(ret_wo.getWo_dsc());
			oaryList.add(fdpaplysoOA);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
	}
	
	
	public void addFnc(FdpaplysoI inTrx,FdpaplysoO outTrx){
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<FdpaplysoIA> iary=inTrx.getIary();
		String cusIdFk=iary.get(0).getCus_id_fk();
		String woCate=iary.get(0).getWo_cate();
		String woId = generateCode(woCate);
		String so_id = iary.get(0).getSo_id();
		String mdlIdFk=iary.get(0).getMdl_id_fk();
		Integer plnPrdQty = iary.get(0).getPln_prd_qty();
		Timestamp plnStbTimestamp =iary.get(0).getPln_stb_timestamp();
		Timestamp plncmpTimestamp =iary.get(0).getPln_cmp_timestamp();
		String woNote =iary.get(0).getWo_note();
		String woDsc = iary.get(0).getWo_dsc();
		String evtUsr= inTrx.getEvt_usr();
		
		//先判断内部订单号有没有重复的
		Ret_wo ret_wo = retWODao.get(woId);
		if (ret_wo != null) {
			outTrx.setRtn_code(E_FDPAPLYSO_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("内部订单号已经存在");
			return;
		}
		//同一个客户订单同一个产品只能有一笔记录
		String hql ="FROM Ret_wo where wo_typ = 'S' and so_id = '"+so_id+"' and mdl_id_fk = '"+mdlIdFk+"'";
		List<Ret_wo> retWoList = retWODao.find(hql);
		if(retWoList.size()>0){
			outTrx.setRtn_code(E_FDPAPLYSO_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("客户订单["+so_id+"]的产品["+mdlIdFk+"]已经存在");
			return;
		}
		Ret_wo newRetWo = new Ret_wo();
		newRetWo.setCus_id_fk(cusIdFk);
		newRetWo.setWo_id(woId);
		newRetWo.setWo_cate(woCate);
		newRetWo.setSo_id(so_id);
		
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdlIdFk);
		Double okRatio = bis_mdl_def.getOk_ratio();//产品良率
		Double toThickness= bis_mdl_def.getTo_thickness();
		String cusMdlId = bis_mdl_def.getCus_mdl_id();
		newRetWo.setMdl_id_fk(mdlIdFk);
		newRetWo.setOk_ratio(okRatio);
		newRetWo.setTo_thickness(toThickness);
		newRetWo.setCus_mtrl_id_fk(cusMdlId);
		newRetWo.setPln_prd_qty(plnPrdQty);
		newRetWo.setPln_stb_timestamp(plnStbTimestamp);
		newRetWo.setPln_cmp_timestamp(plncmpTimestamp);
		newRetWo.setWo_note(woNote);
		newRetWo.setWo_dsc(woDsc);
		//设置为S
		newRetWo.setWo_typ("S");
		//设置订单日期 CRT_TIMESTAMP
		newRetWo.setCrt_timestamp(current_timestamp);
		newRetWo.setWo_stat(WO_STATUS_WAIT);
		newRetWo.setPlant_id("TODO");
		newRetWo.setRl_prd_qty(0);
		newRetWo.setComp_prd_qty(0);
		newRetWo.setWh_in_prd_qty(0);
		newRetWo.setScrp_prd_qty(0);
		newRetWo.setRl_prd_qty(0);
		newRetWo.setComp_prd_qty(0);
		newRetWo.setWh_in_prd_qty(0);
		newRetWo.setWh_out_prd_qty(0);
		newRetWo.setScrp_prd_qty(0);
		newRetWo.setWo_prty("9");
		newRetWo.setCrt_usr(evtUsr);
		newRetWo.setEvt_usr(evtUsr);
		newRetWo.setEvt_timestamp(current_timestamp);
		retWODao.save(newRetWo);
		
		List<FdpaplysoOA> oary = new ArrayList<FdpaplysoOA>();
		FdpaplysoOA fdpaplysoOA = new FdpaplysoOA();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示毫秒
		fdpaplysoOA.setCrt_timestamp(df.format(newRetWo.getCrt_timestamp()));
		fdpaplysoOA.setWo_stat(newRetWo.getWo_stat());
		fdpaplysoOA.setCus_id_fk(newRetWo.getCus_id_fk());
		fdpaplysoOA.setSo_id(newRetWo.getSo_id());
		fdpaplysoOA.setWo_id(newRetWo.getWo_id());
		fdpaplysoOA.setMtrl_prod_id_fk(newRetWo.getMtrl_prod_id_fk());
		fdpaplysoOA.setMdl_id_fk(newRetWo.getMdl_id_fk());
		fdpaplysoOA.setPln_prd_qty(newRetWo.getPln_prd_qty());
		fdpaplysoOA.setPln_cmp_timestamp(df.format(newRetWo.getPln_cmp_timestamp()));
		fdpaplysoOA.setPln_stb_timestamp(df.format(newRetWo.getPln_stb_timestamp()));
		fdpaplysoOA.setRl_prd_qty(newRetWo.getRl_prd_qty());
		fdpaplysoOA.setComp_prd_qty(newRetWo.getComp_prd_qty());
		fdpaplysoOA.setWh_in_prd_qty(newRetWo.getWh_in_prd_qty());
		fdpaplysoOA.setWh_out_prd_qty(newRetWo.getWh_out_prd_qty());
		fdpaplysoOA.setScrp_prd_qty(newRetWo.getScrp_prd_qty());
		fdpaplysoOA.setWo_note(newRetWo.getWo_note());
		fdpaplysoOA.setWo_dsc(newRetWo.getWo_dsc());
		oary.add(fdpaplysoOA);
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
	
	private boolean checkSoPlnPrdQty(Ret_wo retSo){
		String hql ="FROM Ret_wo where wo_typ = 'W' and so_id = '"+retSo.getWo_id()+"'";
		int soPlnPrdQty = retSo.getPln_prd_qty();
		int totalWOsPlnPrdQty = 0;
		List<Ret_wo> retWoList = retWODao.find(hql);
		for(Ret_wo retWo:retWoList){
			totalWOsPlnPrdQty = totalWOsPlnPrdQty + retWo.getPln_prd_qty();
		}
		if(soPlnPrdQty < totalWOsPlnPrdQty){
			return false;
		}
		return true;
	}
	
	
	public void updateFnc(FdpaplysoI inTrx,FdpaplysoO outTrx){
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		String evtUsr = inTrx.getEvt_usr();
		List<FdpaplysoIA> iary=inTrx.getIary();
		String cusIdFk=iary.get(0).getCus_id_fk();
		String woId=iary.get(0).getWo_id();
		String soId=iary.get(0).getSo_id();
		String woCate=iary.get(0).getWo_cate();
		String mdlIdFk=iary.get(0).getMdl_id_fk();
		Integer plnPrdQty = iary.get(0).getPln_prd_qty();
		Timestamp plnStbTimestamp =iary.get(0).getPln_stb_timestamp();
		Timestamp plncmpTimestamp =iary.get(0).getPln_cmp_timestamp();
		String woNote =iary.get(0).getWo_note();
		String woDsc = iary.get(0).getWo_dsc();
		
		//先判断存不存在
		Ret_wo ret_wo = retWODao.get(woId);
		if (ret_wo == null) {
			outTrx.setRtn_code(E_FDPAPLYSO_NOT_FOUND + "");
			outTrx.setRtn_mesg("record not found");
			return;
		}
		//同一个客户订单同一个产品只能有一笔记录
		if(!mdlIdFk.equals(ret_wo.getMdl_id_fk())){
			String hql ="FROM Ret_wo where wo_typ = 'S' and so_id = '"+soId+"' and mdl_id_fk = '"+mdlIdFk+"'";
			List<Ret_wo> retWoList = retWODao.find(hql);
			if(retWoList.size()>0){
				outTrx.setRtn_code(E_FDPAPLYSO_ALREADY_EXIST + "");
				outTrx.setRtn_mesg("客户订单["+soId+"]的产品["+mdlIdFk+"]已经存在");
				return;
			}
		}
		if(checkSoPlnPrdQty(ret_wo)==false){
			outTrx.setRtn_code(E_FDPAPLYSO_OTHER_ERROR+"");
			outTrx.setRtn_mesg("内部订单数量不能小于工单数量之和");
			return ;
		}
		
		if (soId != null) {
			ret_wo.setSo_id(soId);
		}
		if (cusIdFk != null) {
			ret_wo.setCus_id_fk(cusIdFk);
		}
		if (woCate != null) {
			ret_wo.setWo_cate(woCate);
		}
		if (mdlIdFk != null) {
			ret_wo.setMdl_id_fk(mdlIdFk);;
		}
		if (plnPrdQty != null) {
			ret_wo.setPln_prd_qty(plnPrdQty);
		}
		if (plnStbTimestamp != null) {
			ret_wo.setPln_stb_timestamp(plnStbTimestamp);;
		}
		if (plncmpTimestamp != null) {
			ret_wo.setPln_cmp_timestamp(plncmpTimestamp);;
		}
		if (woNote != null) {
			ret_wo.setWo_note(woNote);
		}
		if (woDsc != null) {
			ret_wo.setWo_dsc(woDsc);
		}
		ret_wo.setEvt_usr(evtUsr);
		ret_wo.setEvt_timestamp(current_timestamp);
		retWODao.update(ret_wo);
		
		List<FdpaplysoOA> oary = new ArrayList<FdpaplysoOA>();
		FdpaplysoOA fdpaplysoOA = new FdpaplysoOA();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		fdpaplysoOA.setCrt_timestamp(df.format(ret_wo.getCrt_timestamp()));
		fdpaplysoOA.setWo_stat(ret_wo.getWo_stat());
		fdpaplysoOA.setCus_id_fk(ret_wo.getCus_id_fk());
		fdpaplysoOA.setWo_id(ret_wo.getWo_id());
		fdpaplysoOA.setSo_id(ret_wo.getSo_id());
		fdpaplysoOA.setMtrl_prod_id_fk(ret_wo.getMtrl_prod_id_fk());
		fdpaplysoOA.setMdl_id_fk(ret_wo.getMdl_id_fk());
		fdpaplysoOA.setPln_prd_qty(ret_wo.getPln_prd_qty());
		fdpaplysoOA.setPln_cmp_timestamp(df.format(ret_wo.getPln_cmp_timestamp()));
		fdpaplysoOA.setPln_stb_timestamp(df.format(ret_wo.getPln_stb_timestamp()));
		fdpaplysoOA.setRl_prd_qty(ret_wo.getRl_prd_qty());
		fdpaplysoOA.setComp_prd_qty(ret_wo.getComp_prd_qty());
		fdpaplysoOA.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty());
		fdpaplysoOA.setWh_out_prd_qty(ret_wo.getWh_out_prd_qty());
		fdpaplysoOA.setScrp_prd_qty(ret_wo.getScrp_prd_qty());
		fdpaplysoOA.setWo_note(ret_wo.getWo_note());
		fdpaplysoOA.setWo_dsc(ret_wo.getWo_dsc());
		oary.add(fdpaplysoOA);
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
	
	public void deleteFnc(FdpaplysoI inTrx,FdpaplysoO outTrx){
		FdpaplysoIA iaryA = inTrx.getIary().get(0);
		String woId=iaryA.getWo_id();
		Ret_wo ret_wo=retWODao.get(woId);
		if (ret_wo == null) {
			outTrx.setRtn_code(E_FDPAPLYSO_NOT_FOUND + "");
			outTrx.setRtn_mesg("record not found");
			return;
		}
		if(WO_STATUS_WAIT.equals(ret_wo.getWo_stat())){
			retWODao.delete(ret_wo);
		}else{
			outTrx.setRtn_code(E_FDPAPLYSO_STAT_ERROR + "");
			outTrx.setRtn_mesg("内部订单["+woId+"]的状态为"+ret_wo.getWo_stat()+",不可删除！");
			return;
		}
		String hql ="From Ret_wo where 1 = 1 and wo_typ = 'W'";
		hql = hql + " and so_id = '" + woId +"'";
		List<Ret_wo> ret_wos = retWODao.find(hql);
		List<String> woIds = new ArrayList<String>();
		if(ret_wos.size() > 0){
			for (Ret_wo ret_wo2 : ret_wos) {
				String woID= ret_wo2.getWo_id();
				woIds.add(woID);
			}
			String str="";
			for (int i = 0; i < woIds.size() - 1; i++) {
				str = str + "'" + woIds.get(i).toString().trim() + "',";
			}
			str = str + "'" + woIds.get(woIds.size() - 1).toString().trim() + "'";
			outTrx.setRtn_code(E_FDPAPLYSO_DELETE_ERROR + "");
			outTrx.setRtn_mesg("如要删除该内部订单["+woId+"],则需先删除已绑定工单["+str+"]");
			return;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
	
	public void bindSoId(FdpaplysoI inTrx,FdpaplysoO outTrx){
		String evtUsr = inTrx.getEvt_usr();
		Timestamp currentTime = Util.getCurrentTimestamp();
		FdpaplysoIA iary = inTrx.getIary().get(0);
		String soId = iary.getSo_id();
		String woId = iary.getWo_id();
		
		Ret_wo ret_wo =	retWODao.get(woId);
		if(ret_wo == null){
			outTrx.setRtn_code(E_FDPAPLYSO_NOT_FOUND + "");
			outTrx.setRtn_mesg("record not found");
			return;
		}
		String soID = ret_wo.getSo_id();
		Integer rlPrdQty = ret_wo.getRl_prd_qty();
		if(rlPrdQty > 0 && null != soID && soID.length()!=0){
			outTrx.setRtn_code(E_FDPAPLYSO_BIND_ERROR + "");
			outTrx.setRtn_mesg("内部订单["+woId+"]已绑定过客户订单["+soID+"]并已下线，不可再次绑定！");
			return;
		}
		ret_wo.setEvt_usr(evtUsr);
		ret_wo.setEvt_timestamp(currentTime);
		ret_wo.setSo_id(soId);
		retWODao.update(ret_wo);
		List<FdpaplysoOA> oary = new ArrayList<FdpaplysoOA>();
		FdpaplysoOA fdpaplysoOA = new FdpaplysoOA();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示毫秒
		fdpaplysoOA.setCrt_timestamp(df.format(ret_wo.getCrt_timestamp()));
		fdpaplysoOA.setWo_stat(ret_wo.getWo_stat());
		fdpaplysoOA.setCus_id_fk(ret_wo.getCus_id_fk());
		fdpaplysoOA.setWo_id(ret_wo.getWo_id());
		fdpaplysoOA.setSo_id(ret_wo.getSo_id());
		fdpaplysoOA.setMtrl_prod_id_fk(ret_wo.getMtrl_prod_id_fk());
		fdpaplysoOA.setFrom_thickness(ret_wo.getFrom_thickness().toString());
		fdpaplysoOA.setMdl_id_fk(ret_wo.getMdl_id_fk());
		fdpaplysoOA.setPln_prd_qty(ret_wo.getPln_prd_qty());
		fdpaplysoOA.setPln_cmp_timestamp(df.format(ret_wo.getPln_cmp_timestamp()));
		fdpaplysoOA.setPln_stb_timestamp(df.format(ret_wo.getPln_stb_timestamp()));
		oary.add(fdpaplysoOA);
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
	
	public void errActionFnc(FdpaplysoO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
		return;
	}
	
	public String generateCode(String woCate){
		String code ="";
		int i =0;
		char[] Digit = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J', 'K', 'L', 'M', 'N','O','P', 'Q', 'R', 'S',
				'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
		Timestamp currentTime= Util.getCurrentTimestamp();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sf.format(currentTime);
		String timeCode = String.join("",time.split("-")).substring(2);
		String cate= woCate.substring(0,1);
		
		String likeCode = "W"+cate+timeCode+"A";
		String hql = "From Ret_wo where wo_id like '%" +likeCode+"%'";
		List<Ret_wo> ret_wos = retWODao.find(hql);
		
		if(ret_wos.size() > 0){
			List<Byte> bytes = new ArrayList<Byte>();
			for (Ret_wo ret_wo : ret_wos) {
				String woId = ret_wo.getWo_id();
				char ch = woId.charAt(woId.length()-1);
				byte b =(byte)ch;
				bytes.add(b);
			}
			byte mByte= Collections.max(bytes);
			char mch = (char)mByte;
			if(mch == Digit[8]){
				mByte = 65;
				mch =(char)mByte;
				code = likeCode+mch;
				return code;
			}
			mByte++;
			mch =(char)mByte;
			code = likeCode+mch;
			return code;
		}else{
			i = 0;
			code = likeCode + Digit[i];
			return code;
		}
	}
	
}
